package com.poisonh.unifiedauth.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.utils.CommonUtils;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.PreferencesUtils;
import com.poisonh.unifiedauth.utils.RegexUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 绑定邮箱
 * Created by PoisonH on 2016/6/28.
 */
public class BindEmailActivity extends BaseActivity implements View.OnClickListener
{
    private EditText mMetBindInputEmail;
    private EditText mMetBindInputEmailCode;
    private Button mBtnSendCode;
    private Button mBtnCommitEmail;
    private String mStrEmail;
    private String mStrEmailCode;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_bindemail);
        initTitleView();
        setTitleTxt("绑定邮箱");
        mStrEmail = getIntent().getStringExtra("EMAIL");
        initView();
    }

    /**
     * 初始化视图
     */
    @Override
    protected void initView()
    {
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mMetBindInputEmail = (EditText) findViewById(R.id.bindemail_input_email);
        mMetBindInputEmailCode = (EditText) findViewById(R.id.bindemail_input_emailcode);
        if (mStrEmail != null)
        {
            mMetBindInputEmail.setText(mStrEmail);
            Editable etext = mMetBindInputEmail.getText();
            Selection.setSelection(etext, etext.length());
        }
        mBtnSendCode = (Button) findViewById(R.id.btn_send_emailcode);
        mBtnCommitEmail = (Button) findViewById(R.id.btn_commit_eamil);
        mBtnSendCode.setOnClickListener(this);
        mBtnCommitEmail.setOnClickListener(this);
        mMetBindInputEmailCode.addTextChangedListener(mEmailCodeTextWatcher);
        sendEmailCode();
        mBtnSendCode.setEnabled(false);
        CommonUtils.CountdownTime(mBtnSendCode);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
            case R.id.btn_send_emailcode:
                sendEmailCode();
                mBtnSendCode.setEnabled(false);
                CommonUtils.CountdownTime(mBtnSendCode);
                break;
            case R.id.btn_commit_eamil:
                mStrEmailCode = mMetBindInputEmailCode.getText().toString();
                if (mStrEmailCode.isEmpty() || mStrEmailCode.length() != 6)
                {
                    ToastUtils.showToast(this, "请输入正确的邮箱验证码!");
                } else
                {
                    bindEmail();
                }
                break;
        }
    }

    /**
     * 发送邮箱验证码
     */
    private void sendEmailCode()
    {
        HttpParams params = new HttpParams();
        //  params.put("id", ConstantUitls.mStrId);
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("email", mStrEmail);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.SENDMAIL, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(BindEmailActivity.this, failReason);
                mBtnSendCode.setText(R.string.login_tips_btn_phonecode);
                mBtnSendCode.setEnabled(true);
            }
        });
    }

    /**
     * 绑定邮箱
     */
    private void bindEmail()
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("email", mStrEmail);
        params.put("msgCode", mStrEmailCode);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.VERIFYEMAIL, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                ToastUtils.showToast(BindEmailActivity.this, "绑定邮箱成功");
                try
                {
                    //根据id查询用户
                    UserinfoDaoImpl mUserinfoDaoImpl = new UserinfoDaoImpl(BindEmailActivity.this);
                    UserInfoModel model = mUserinfoDaoImpl.queryReturnOne(mStrUserId);
                    model.setmStrUserEmail(mStrEmail);
                    mUserinfoDaoImpl.commit();
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
                finish();
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(BindEmailActivity.this, failReason);
            }
        });
    }

    TextWatcher mEmailCodeTextWatcher = new TextWatcher()
    {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

        }

        @Override
        public void afterTextChanged(Editable s)
        {
            if (temp.length() == 6)
            {
                mBtnCommitEmail.setEnabled(true);
                mBtnCommitEmail.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg));
            } else
            {
                mBtnCommitEmail.setEnabled(false);
                mBtnCommitEmail.setBackground(getResources().getDrawable(R.drawable.shape_settingmyinfo_btn_bg_));
            }
        }
    };
}
